' ##############################################################################
' Declarations
' ##############################################################################
const sqlite3_ok		= 0	' successful result
' I erased other error constants - you can find it on "www.sqlite.org"

declare function sqlite3_libversion LIB "sqlite3.dll" alias "sqlite3_libversion" () as long
declare function sqlite3_open LIB "sqlite3.dll" alias "sqlite3_open" _
	(byref filename as string, byref db as long) as long
declare function sqlite3_close LIB "sqlite3.dll" alias "sqlite3_close" (db as long) as long
declare function sqlite3_exec LIB "sqlite3.dll" alias "sqlite3_exec" _
	(db as long, byref sql as string, pCallback as long, pUserData as long, ppErrMsg as long) as long
declare function sqlite3_get_table LIB "sqlite3.dll" alias "sqlite3_get_table" _
	(db as long, sql as string, lpTable as long, nRow as long, nColumn as long, lpErrMsg as long) as long
declare function sqlite3_free_table LIB "sqlite3.dll" alias "sqlite3_free_table" _
	(lpTable as long) as long
'declare function sqlite3_last_insert_rowid lib "sqlite3.dll" alias "sqlite3_last_insert_rowid" _
'		(byval db as long) as long ' I am not sure, if last LONG is OK

' ------------------------------------------------------------------------------
function SQL3_libversion
	dim bChar			as byte
	dim iCounter		as byte
	dim ptr_version		as long
	dim str_version		as string
		str_version = ""

	ptr_version = sqlite3_libversion()
	iCounter = 0
	DO
        MemCpy (varptr(bChar), ptr_version + iCounter, 1)
		IF (bChar = 0) THEN EXIT DO
	    str_version = str_version + chr$(bChar)
        iCounter++
    LOOP
	SQL3_libversion = str_version
end function

' ------------------------------------------------------------------------------
function SQL3_gettable
' this part was rewriten and repair, it enabled to use many cols and rows!!!
	dim result1	as long
	dim sql_cmd	as string
	dim nrows	as long
	dim ncolumns	as long
	dim perrmsg	as long
	dim lptable	as long
	dim lerrmsg	as long
	dim ipos	as integer
	dim bcar	as byte
	dim litems	as long
	dim prow	as long
	dim pcol	as long
	dim sversion	as string
	dim icounter	as long
	dim separ as String
	dim StringList as QStringList
		StringList.Clear

	result1 = sqlite3_open (filename, db)
	if result1 = sqlite3_ok then
		result1 = sqlite3_get_table (db, sql_cmd, VARPTR(lptable), VARPTR(nrows), VARPTR(ncolumns), VARPTR(perrmsg))
		if result1 = sqlite3_ok then
			for litems = 0 to (nrows * ncolumns) + (ncolumns - 1)
				pcol = (litems mod ncolumns)
				if pcol = 0 then sversion = ""
				memcpy varptr(prow), lptable + (4 * litems), 4
				icounter = 0

				do
					memcpy varptr(bcar), prow + icounter, 1
					if (bcar = 0) then exit do
					sversion = sversion + chr$(bcar)
					icounter = icounter + 1
				loop

				if pcol = ncolumns - 1 and bcar = 0 then
					stringlist.additems sversion
				else:
					sversion = sversion + separ
				end if
			next
			sqlite3_free_table varptr(lptable)
		else
			showmessage SQL3_errCode(lerrmsg)
		end if
		SQL3_gettable = stringList.text
	end if
	sqlite3_close (db)
end function

' ------------------------------------------------------------------------------
function SQL3_errcode
	' others error numbers are missing (could someone help me with testing?)
	dim str_temp as String
	dim Error as Word
	Select case Error
		Case 1: str_Temp = "wrong SQL syntax or missing database..."
		Case 19: str_Temp = "the same field on UNIQUE field..."
		Case Else:
			str_Temp = "unknown error - please describe it to the author (iss@post.sk)"
	end Select
	SQL3_errCode = "(" + str$(Error) + ") - SQLite3 database error. Perhaps mistake: " _
		+ chr$(13) +  str_Temp
end function
